package com.gisocn.onemap.presenter;

import android.util.Log;
import com.gisocn.onemap.base.BasePresenter;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.interfaces.DownLoadListener;
import com.gisocn.onemap.model.GetDirectoryData;
import com.gisocn.onemap.model.GetDirectoryVersion;
import com.gisocn.onemap.net.ApiCallback;
import com.gisocn.onemap.utils.ExecutorsManager;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.view.PlanDataBaseView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

/* loaded from: classes2.dex */
public class PlanDataBasePresent extends BasePresenter<PlanDataBaseView> {
    HttpURLConnection conn;
    String savaFileNamePath;
    private URL url = null;

    public PlanDataBasePresent(PlanDataBaseView planDataBaseView) {
        attachView(planDataBaseView);
    }

    public void getDirectoryVersion(Map<String, String> map) {
        addSubscription(this.apiService.getDirectoryVersion(map), new ApiCallback<GetDirectoryVersion>() { // from class: com.gisocn.onemap.presenter.PlanDataBasePresent.1
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(GetDirectoryVersion getDirectoryVersion) {
                if (getDirectoryVersion.getReturnCode() == 0) {
                    ((PlanDataBaseView) PlanDataBasePresent.this.mvpView).getDirectoryVersionSuccess(getDirectoryVersion.getMessage());
                } else {
                    ((PlanDataBaseView) PlanDataBasePresent.this.mvpView).fail(getDirectoryVersion.getMessage());
                }
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str) {
                ((PlanDataBaseView) PlanDataBasePresent.this.mvpView).fail(str);
            }
        });
    }

    public void getDirectoryData(Map<String, String> map) {
        addSubscription(this.apiService.getDirectoryData(map), new ApiCallback<GetDirectoryData>() { // from class: com.gisocn.onemap.presenter.PlanDataBasePresent.2
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(GetDirectoryData getDirectoryData) {
                if (getDirectoryData.getReturnCode() == 0) {
                    ((PlanDataBaseView) PlanDataBasePresent.this.mvpView).getDirectoryDataSuccess(getDirectoryData);
                } else {
                    ((PlanDataBaseView) PlanDataBasePresent.this.mvpView).fail(getDirectoryData.getMessage());
                }
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str) {
                ((PlanDataBaseView) PlanDataBasePresent.this.mvpView).fail(str);
            }
        });
    }

    public void getDownLoading(final String str, final String str2, final String str3, final boolean z, final int i, final DownLoadListener downLoadListener) {
        L.e("getDownLoading_fileUrl" + str);
        ExecutorsManager.getInstance().execute(new Runnable() { // from class: com.gisocn.onemap.presenter.PlanDataBasePresent.3
            @Override // java.lang.Runnable
            public void run() {
                String str4;
                try {
                    try {
                        str4 = new URI(str).toASCIIString();
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                        str4 = "";
                    }
                    PlanDataBasePresent.this.url = new URL(str4);
                    Log.e("ASCIIUrl", PlanDataBasePresent.this.url.getPath());
                    try {
                        PlanDataBasePresent planDataBasePresent = PlanDataBasePresent.this;
                        planDataBasePresent.conn = (HttpURLConnection) planDataBasePresent.url.openConnection();
                        File file = new File(Constant.PLAN_DATA_BASE_PATH + str2);
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        PlanDataBasePresent.this.savaFileNamePath = Constant.PLAN_DATA_BASE_PATH + str2 + str3;
                        File file2 = new File(PlanDataBasePresent.this.savaFileNamePath);
                        if (!file2.exists()) {
                            file2.createNewFile();
                        }
                        Log.e("downLoadFile", "downLoadFile");
                        downLoadListener.onStart();
                        try {
                            InputStream inputStream = PlanDataBasePresent.this.conn.getInputStream();
                            long contentLength = PlanDataBasePresent.this.conn.getContentLength();
                            long j = 0;
                            if (contentLength != 0 && contentLength != -1) {
                                FileOutputStream fileOutputStream = new FileOutputStream(file2);
                                byte[] bArr = new byte[1024];
                                while (true) {
                                    int read = inputStream.read(bArr);
                                    if (read != -1) {
                                        fileOutputStream.write(bArr, 0, read);
                                        j += read;
                                        long j2 = 100 * j;
                                        downLoadListener.onProgress((int) (j2 / contentLength));
                                        if (((int) (j2 / contentLength)) == 100) {
                                            downLoadListener.onFinish(PlanDataBasePresent.this.savaFileNamePath);
                                            if (z) {
                                                ((PlanDataBaseView) PlanDataBasePresent.this.mvpView).getDownLoadSuccess(PlanDataBasePresent.this.savaFileNamePath, i);
                                            } else {
                                                ((PlanDataBaseView) PlanDataBasePresent.this.mvpView).getDownLoadSuccess(PlanDataBasePresent.this.savaFileNamePath);
                                            }
                                        }
                                    } else {
                                        inputStream.close();
                                        fileOutputStream.close();
                                        return;
                                    }
                                }
                            }
                            Log.e("totalLength", "" + contentLength);
                            downLoadListener.onFailure();
                        } catch (FileNotFoundException e2) {
                            e2.printStackTrace();
                        } catch (IOException e3) {
                            e3.printStackTrace();
                        }
                    } catch (IOException e4) {
                        e4.printStackTrace();
                    }
                } catch (MalformedURLException e5) {
                    e5.printStackTrace();
                }
            }
        });
    }
}
